<template>
  <div style="padding:10px 30px">
    <!-- 多选框 -->
    <el-checkbox-group v-model="checkList">
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>

    <el-row type="flex" justify="end" align="middle" style="margin-top:20px">
      <el-button type="primary" @click="submitRole">确认</el-button>
    </el-row>
  </div>

</template>

<script>
import { queryRoleList, saveAdssignRole } from '@/api/settings'
import { getUserDetailById } from '@/api/user'
export default {
  name: '',
  props: {
    curId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      // 角色列表
      roleList: [],
      // 被选中的选框
      checkList: []
    }
  },
  created() {
    this.getRoleList()
    this.getRoleByCur()
  },

  methods: {
    // ! 1.获取到所有的角色信息
    async getRoleList() {
      const res = await queryRoleList({ page: 1, pagesize: 999 })
      // console.log(res)
      this.roleList = res.data.rows
    },
    //! 2,.获取当前员工的角色信息
    async getRoleByCur() {
      const res = await getUserDetailById(this.curId)
      console.log(res)
      this.checkList = res.data.roleIds
    },
    // ! 3.保存当前权限
    async submitRole() {
      const res = await saveAdssignRole({ id: this.curId, roleIds: this.checkList })
      console.log(res)
      this.$message.success(res.message)
      this.$emit('successAssignRole')
    }
  }
}
</script>

<style scoped>

</style>
